package com.sl.au.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.Chance;
import com.sl.au.entity.ChanceFlowNode;
import com.sl.au.entity.User;
import com.sl.au.repository.ChanceFlowNodeRepository;
import com.sl.au.service.ChanceFlowNodeService;

@Service("chanceFlowNodeService")
@Transactional
public class ChanceFlowNodeServiceImpl extends BaseServiceImpl implements ChanceFlowNodeService {
	@Autowired
	ChanceFlowNodeRepository chanceFlowNodeRepository;
	
	
	@Override
	public ChanceFlowNodeRepository getRepository() {
		
		return this.chanceFlowNodeRepository;
	}

	@Override
	public void flow(User user, Chance saleChance,String startNode, String endNode,String flowOperate) {
		// TODO Auto-generated method stub
		ChanceFlowNode node=new ChanceFlowNode();
		node.setStartNode(startNode);
		node.setEndNode(endNode);
		node.setFlowOperate(flowOperate);
		node.setChance(saleChance);
		node.setUser(user);
		chanceFlowNodeRepository.save(node);
	}

	@Override
	public void flow(User user, Chance saleChance,String startNode, String endNode,String flowOperate,String reason) {
		// TODO Auto-generated method stub
		ChanceFlowNode node=new ChanceFlowNode();
		node.setStartNode(startNode);
		node.setEndNode(endNode);
		node.setFlowOperate(flowOperate);
		node.setChance(saleChance);
		node.setUser(user);
		node.setReason(reason);
		chanceFlowNodeRepository.save(node);
	}
	
	@Override
	public List<ChanceFlowNode> getFlows(String chanceId) {
		Page<ChanceFlowNode> pages = null;

		Pageable pageable = new PageRequest(0 / 20, 20, Sort.Direction.ASC,"createTime");
		
		pages=chanceFlowNodeRepository.findFlows(chanceId, pageable);

		return pages.getContent();
	}

}
